package com.thd.springboottest.mybatis.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.thd.springboottest.mybatis.dao.MyUserMapper;
import com.thd.springboottest.mybatis.dao.SysUserMapper;
import com.thd.springboottest.mybatis.entity.MyUser;
import com.thd.springboottest.mybatis.entity.SysUser;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author devil13th
 **/
@Service
@Transactional
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private MyUserMapper myUserMapper;

    @Autowired
    private SqlSessionFactory sqlSessionFactory;
    // 根据id查询用户
    public SysUser getByUserId(String id){
        return sysUserMapper.selectById(id);
    }
    //获取全部用户
    public List<SysUser> getAll(){
        return sysUserMapper.selectAll();
    }
    @Transactional
    //保存用户
    public int insert(SysUser user){
        return sysUserMapper.insert(user);
    }


    public List<SysUser> queryByName(String name){
        return sysUserMapper.queryByName(name);
    }

    // 分页查询
    public PageInfo<SysUser> queryByNamePage(String username, int limit, int page){
        PageHelper.startPage(page, limit).setOrderBy("user_name desc");
        PageInfo<SysUser> userPageInfo = new PageInfo<SysUser>(sysUserMapper.queryByName(username));
        return userPageInfo;
    }

    public void insertBigData(){
        System.out.println(Thread.currentThread().getName() + ": 开始" );
        int ct = 1000;
        List<SysUser>  l = new ArrayList<SysUser>(ct);
        int i = 0;
        while(i<ct){
            SysUser su = new SysUser();
            su.setUserId(UUID.randomUUID().toString().replace("-",""));
            su.setUserBirthday(new Date());
            su.setUserMail("user" + i + "@163.com");
            su.setUserName("user_" + i);
            su.setUserSex(1);
            su.setUserStatus("1");
            su.setUserTel("12345678");
            l.add(su);
            i++;
        }
        this.sysUserMapper.insertBatch(l);
        System.out.println(Thread.currentThread().getName() + ": 结束" );
    };

    // 测试缓存
    public void testCache(){

        /**
         * 缓存失效原因:
         *  1.不同的SqlSession
         *  2.两次相同的查询之间有更新/删除语句,第二次查询就不会有缓存了
         *  3.清理了缓存
         */
        // 会发送SQL语句查询
        SysUser user = this.sysUserMapper.selectById("1");
        // 不会查询了，因为MyBaits的一级缓存(去掉该类的@Transactional后才会查询,因为SqlSession不一样了)
        SysUser user2 = this.sysUserMapper.selectById("1");
        // 不会查询了，因为MyBaits的一级缓存(去掉该类的@Transactional后才会查询,因为SqlSession不一样了)
        SysUser user3 = this.sysUserMapper.selectById("1");



        // 验证1.去掉该类的@Transactional后才会查询,因为SqlSession不一样了

        // 验证2.两次相同的查询之间有更新/删除语句,第二次查询就不会有缓存了
//        SysUser sysUser = new SysUser();
//        sysUser.setUserId("1");
//        sysUser.setUserName("zhaosan");
//        this.updateNameById(sysUser);

        // 验证2. 两次相同的查询之间有更新/删除语句,第二次查询就不会有缓存了
//        MyUser u = new MyUser();
//        u.setUserId("aa2f36be-26fe-4214-aa88-55a5d552bfe9");
//        u.setUserName("lisi");
//        myUserMapper.update(u);

        // 验证3 清理缓存
        SqlSessionUtils.getSqlSession(sqlSessionFactory).clearCache();






        // 会查询，因为上面执行了更新的SQL语句
        SysUser user4 = this.sysUserMapper.selectById("1");


    };

    public void updateNameById(SysUser sysUser){
        this.sysUserMapper.updateNameById(sysUser);
    };


    @Override
    @Transactional(propagation = Propagation.NESTED)
    public void testNestedTransaction() {
        SysUser user = this.getByUserId("1");
        user.setUserName(Integer.parseInt(user.getUserName()) + 1 + "");
        this.sysUserMapper.updateNameById(user);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void testNewTransaction() {
        SysUser user = this.getByUserId("1");
        user.setUserName(Integer.parseInt(user.getUserName()) + 1 + "");
        this.sysUserMapper.updateNameById(user);

        int a = 1/0;
    }
}
